/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 17:58:05
 * describe:下线
 */
APP.controller('OutlineController', ['$scope', '$rootScope', 'OutlineService', '$state', '$ionicPopup', '$timeout', 'PopupService',
    function ($scope, $rootScope, OutlineService, $state, $ionicPopup, $timeout, PopupService) {

        /***变量声明***/
        $scope.data = {
            barCode: ''
        };
        $scope.barcode = '';
        $scope.data = {
            number: '',
            supplier: '',
            paper: '',
            residueKg: '',//剩余重量
            weight: '',//克重
            specification: '',//规格
            kg: ''//重量
        };
        $scope.outLineData = {
            axle: '',
            finish: '',
            length: '',
            residueKg: ''
        };

        /**
         * 初始化函数
         */
        $scope.init = function () {

        };
        //选择轴序
        $scope.goSupplier = function (type) {
            $state.go('supplier', {type: type});
        };

        /**
         * 按轴序查询物料信息
         */
        $scope.getByAxle = function () {
            OutlineService.getByAxle($rootScope.onlineAxle)
                .then(function (response) {
                    console.log('成功回调方法', response);
                    if(!response.data.data) {
                        PopupService.showToast('该轴序无产品');
                        $rootScope.onlineAxle = '';
                        return;
                    }
                    $scope.data = response.data.data;
                    $scope.barcode = response.data.data.barcode.substring(4, response.data.data.barcode.length - 1);
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };
        /**
         * 下线操作
         */
        $scope.outLineAlex = function (ifFinish) {


            $scope.outLineData.axle = $rootScope.onlineAxle;
            $scope.outLineData.finish = ifFinish ? ifFinish : false;
            if (!$scope.outLineData.axle) {
                PopupService.showToast('请选择轴序');
                return;
            }
            OutlineService.outLine($scope.outLineData)
                .then(function (response) {
                    console.log('成功回调方法', response);
                    if (response.data.success) {
                        PopupService.showToast('下线成功');
                        $scope.residueKg = "^B4" + $scope.outLineData.residueKg + "kg";
                        $scope.isPrint = true;
                        $scope.printContent("^B4" + $scope.barcode);
                        $scope.data = {
                            number: '',
                            supplier: '',
                            paper: '',
                            residueKg: '',//剩余重量
                            weight: '',//克重
                            specification: '',//规格
                            kg: ''//重量
                        };
                        $scope.outLineData = {
                            axle: '',
                            finish: '',
                            length: '',
                            residueKg: ''
                        };
                        $state.go('main');
                    }
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };

        $scope.warnAllUser = function (id) {
            $scope.outLineData.finish = true;
            // 一个精心制作的自定义弹窗
            // An elaborate, custom popup
            var myPopup = $ionicPopup.show({
                title: '确认用完？',
                scope: $scope,
                buttons: [
                    {
                        text: '<span>确认</span>',
                        type: '',
                        onTap: function () {
                            $scope.outLineAlex(true);
                        }
                    },
                    {text: '放弃'}
                ]
            });
            // myPopup.then(function(res) {
            //     console.log('Tapped!', res);
            // });
            $timeout(function () {
                // myPopup.close();
            }, 3000);
        };

        $scope.printContent = function (code) {
            OutlineService.printCon(code)
                .then(function (response) {
                    $timeout(function () {
                        OutlineService.printCon('^Q +http://weixin.qq.com/r/2Eg2LkzEKRFWrQhN9123')
                            .then(function (response) {
                            }, function (response) {
                                console.log('失败回调方法', response)
                            })
                    }, 1000);
                    $timeout(function () {
                        OutlineService.printCon($scope.residueKg)
                            .then(function (response) {
                            }, function (response) {
                                console.log('失败回调方法', response)
                            })
                    }, 2000);
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };

        /**
         * 计算剩余重量
         */
        $scope.remainKg = function (data) {
            $scope.outLineData.residueKg = Math.floor($scope.data.residueKg - ($scope.outLineData.length * $scope.data.specification / 1000 * $scope.data.weight) / 1000);
        };


        $scope.$on('$ionicView.beforeEnter', function (e, v) {
            if (v.direction == 'back') {
                if ($rootScope.onlineAxle) {
                    $scope.getByAxle();
                }
            } else {
                $rootScope.onlineAxle = '';
                $scope.init();
            }

        });
    }
]);

/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 17:58:05
 * describe:下线
 */
APP.service('OutlineService', ['$http', 'UrlService', function ($http, UrlService) {
    /**
     * 按轴序查询物料信息
     */
    this.getByAxle = function (data) {
        var url = 'http://47.94.130.122/api/material/querybyaxle/' + data;
        console.log(url);
        return $http.get(url);
    };
    /**
     * 下线
     */
    this.outLine = function (data) {
        var params = {
            axle: data.axle,
            finish: data.finish,
            length: data.length,
            residueKg: data.residueKg
        };
        return $http.post(UrlService.getUrl('OFF_LINE'), params);
    };

    this.printCon = function (code) {
        return $http.post('http://open.printcenter.cn:8080/addOrder?deviceNo=kdt1082610&&key=933b8&&printContent=' + code + '&&times=1');
    }
}]);
